{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.common import Currency, PayReceive\n",
    "from gs_quant.instrument import IRSwap\n",
    "from gs_quant.markets import PricingContext, OverlayMarket, MarketDataCoordinate\n",
    "from gs_quant.session import GsSession"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "GsSession.use(client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Retrieve market data "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Let's create a swaption and retrieve the market data our instrument is sensitive to. To do so we can call `market()` on \n",
    "our instrument. This will give us the `OverlayMarket` object which contains the market data used to price our \n",
    "instrument. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "swap = IRSwap(PayReceive.Receive, '10y', Currency.EUR, fixed_rate=-0.025)\n",
    "swap.resolve()\n",
    "market = swap.market()\n",
    "\n",
    "print(f'Base price: {swap.price()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Then, using the `market_data` attribute, we can access the market data coordinates and values directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(f'The value of the coordinate, {market.market_data[0].coordinate} is {market.market_data[0].value}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Overwrite market data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "We can also amend the market data of our instrument's `OverlayMarket` to pass-in our own market data value. \n",
    "To do so, we simply overwrite the `MarketDataCoordinate` of the instrument `OverlayMarket` to a given value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "c_10y = MarketDataCoordinate.from_string('IR_EUR_SWAP_10Y.ATMRATE')\n",
    "\n",
    "print(f'Current value of the EUR 10yr swap point is {market[c_10y]}')\n",
    "\n",
    "market[c_10y] = -0.02\n",
    "\n",
    "print(f'New value of the EUR 10yr swap point is {market[c_10y]}')\n",
    "\n",
    "with PricingContext(market=market):\n",
    "    price_f = swap.price()\n",
    "\n",
    "print(f'New price: {price_f.result()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "... or pass in an new `OverlayMarket` all together! Here we create a bespoke market with our own values for the 3m5y \n",
    "implied volatility and 10y swap rate. Note that the values that are not overwritten will be defaulted to their original \n",
    "value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from gs_quant.instrument import IRSwaption\n",
    "swaption = IRSwaption(PayReceive.Receive, '5y', Currency.EUR, expiration_date='3m')\n",
    "swaption.resolve()\n",
    "\n",
    "print(f'Base price: {swaption.price()}')\n",
    "\n",
    "vol_3m5y = MarketDataCoordinate.from_string('IR VOL_EUR-EURIBOR-TELERATE_SWAPTION_5Y,3M')\n",
    "market_data = {c_10y: 0.01, vol_3m5y: 40 / 1e4}\n",
    "new_market = OverlayMarket(market_data)\n",
    "\n",
    "with PricingContext(market=new_market):\n",
    "    price_f = swaption.price()\n",
    "\n",
    "print(f'Price from new market data: {price_f.result()}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
